package com.yibo.manager.user.service;

import com.github.pagehelper.PageHelper;
import com.yibo.db.helper.BaseService;
import com.yibo.db.helper.QueryFilter;
import com.yibo.db.user.entity.ManagerUser;
import com.yibo.db.user.mapper.manageruser.ManagerUserMapper;
import com.yibo.manager.user.ManagerUserService;
import com.yibo.manager.user.pojo.ManagerUserMsg;
import com.yibo.manager.user.service.manager.LoginManagerCommand;
import com.yibo.manager.user.service.manager.LoginOutCommand;
import com.yibo.support.encry.MD5Helper;
import com.yibo.support.helper.Assert;
import com.yibo.support.helper.ObjectHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

import static com.yibo.support.constant.MsgConstant.USER_HAS_EXIST;
import static com.yibo.support.constant.MsgConstant.USER_MESSAGE_LACK;

@Service
@Transactional
public class ManagerUserServiceImpl extends BaseService implements ManagerUserService {


    @Resource
    private ManagerUserMapper managerUserMapper;

    @Override
    public QueryFilter<List<ManagerUser>> queryList(QueryFilter filter) {
        int pageNum = pageNum(filter);
        int pageSize = pageSize(filter);
        //校验参数
        checkQueryFilter(filter,ManagerUser.class);
        //查询
        PageHelper.startPage(pageNum,pageSize);

        List<ManagerUser>  managerUsers = query(filter,managerUserMapper);
        return new QueryFilter<List<ManagerUser>>().initResult(managerUsers);
    }



    @Override
    public ManagerUser queryOne(String userId) {
        return managerUserMapper.selectById(userId);
    }

    @Override
    public int addUser(ManagerUser managerUser) {
        Assert.hasEmpty(USER_MESSAGE_LACK,
                managerUser.getUserId(),
                managerUser.getPassword(),
                managerUser.getUserName());

        ManagerUser user = managerUserMapper.selectById(managerUser.getUserId());
        Assert.isTrue(USER_HAS_EXIST,!ObjectHelper.isEmpty(user));

        managerUser.setPassword(MD5Helper.getMD5String(managerUser.getPassword()));
        return managerUserMapper.insert(managerUser);
    }

    @Override
    public int updateUser(ManagerUser managerUser) {
        Assert.hasEmpty(USER_MESSAGE_LACK,
                managerUser.getUserId(),
                managerUser.getPassword(),
                managerUser.getUserName());

        ManagerUser user = managerUserMapper.selectById(managerUser.getUserId());
        Assert.isTrue(USER_HAS_EXIST,!ObjectHelper.isEmpty(user) && !managerUser.getUserId().equals(user.getUserId()));

        //更新
        return managerUserMapper.updateById(managerUser);
    }

    @Override
    public int deleteOne(String userId) {
        return managerUserMapper.deleteById(userId);
    }

    @Override
    public ManagerUserMsg login(String userId, String password) {
        return new LoginManagerCommand(userId,password).execute();
    }

    @Override
    public int loginOut() {
        return new LoginOutCommand().execute();
    }
}
